<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传资源</title>
<link rel="stylesheet" href="${rc.contextPath}/css/resource.css" type="text/css" />
<script type="text/javascript" src="${rc.contextPath}/js/jquery/jquery-1.8.2.js"></script>
<script type="text/javascript" src="${rc.contextPath }/js/jquery/jquery.blockUI.js" charset="utf-8"></script>
<script type="text/javascript" src="${rc.contextPath }/js/article/simpleTree.js"></script>
<link rel="stylesheet" href="${rc.contextPath}/css/website.css" type="text/css" />
<style type="text/css">
ul li{ vertical-align: middle; line-height: 20px; list-style: none;}
.ul_tree li{width:98%;min-width:175px;dispaly:inline;white-space:nowrap;font-size:13px;	font-family:'宋体';height:20px;cursor:pointer;}
.ul_tree li img{vertical-align: middle;}
.on_li{font-size:13px;font-family:'宋体';vertical-align:middle;	line-height:20px;height:20px;cursor:pointer;font-weight: bold;color:red;font-style: italic;}
.ul_tree_node li{width:auto !important;min-width:175px;dispaly:inline-block;display:-moz-inline-box; white-space:nowrap; font-size:13px;height:20px;cursor:pointer;margin-left:-16px;}
.ul_tree_node li img{vertical-align: middle;margin-top:0px;}


#targetColumn_box{border:0px solid #ccc;height:38px;width:50%;overflow: auto;overflow-x:hidden;}
</style>
<script type="text/javascript">
function appendText(val){
	if(!val){
		$("#search_key_span").css("display","");
	}else{
		$("#search_key_span").css("display","none");
		$("#searchText").val('');
	}
	if(val == '相册'){
		$("#targetColumn_tr").show(200);
		var $seObj = $("select[name='fsiteid']");
		if($seObj.val() != ''){
			loadColumnSource($seObj[0]);
		}
	}else{
		$("#targetColumn_box").css('height','38px');
		$("#targetColumn_tr").hide();
	}
}

function setSearchKeyVal(val){
	$("[name=fsearchkey]").prepend("<option value='"+val+"'>"+val+"</option>"); //在前面插入一项option
	appendText(val);
	$("[name=fsearchkey] option").eq(0).attr('selected', 'true');
}

function isImage(val){
	if(val=='图片'){
		$("#image").css("display","");
		$("#data").css("display","none");
	}else{
		$("#image").css("display","none");
		$("#data").css("display","");
	}
}

function loadColumnSource(webSite){
	if($("select[name='fsearchkey']").val() == '相册'){
		$("#targetColumn_box").html('<img src="${rc.contextPath }/images/preload.gif" style="margin-left:45%;"/>');
		var local = "/CMS";
		$.ajax({
			url:'/CMS/main/tcolunmSource',
			type:'post',
			data:{'webid':webSite.value,'check':'0','type':0},
			dataType:'json',
			success:function(data){
				_articleCount = 0 ;
				$("#targetColumn_box").html($.fn.simpleTree(data,local,true,webSite.options[webSite.selectedIndex].text,null));
				var timer = setInterval(function(){addTargetDivHeight(document.getElementById("targetColumn_box"),timer);},2);
			},
			error:function(msg){
				$.growlUI('栏目加载出错');
			}
		});
	} 
}
function addTargetDivHeight(obj,timer){
	var h = obj.clientHeight;
	if(h <= 200){
		obj.style.height = (h + 2) + 'px';
	}else{
		clearTimeout(timer);
	}
}
</script>
</head>
<body>
	<form id="uploadForm" action="uploadResource" method="post">
		<input type="hidden" name="columnIdStr" id="columnIdStr"/> 
	  <table border="0" cellspacing="1" class="fu_list">
	    <thead>
	      <tr>
	        <td colspan="2"><b>上传图片资源</b></td>
	      </tr>
	    </thead>
	    <tbody>
	      <tr>
	        <td align="right" width="15%" style="line-height:35px;">添加文件：</td>
	        <td><a href="${(usermenu.freadonly=='w'||usermenu.freadonly=='wc')?'javascript:void(0);':''}" class="${(usermenu.freadonly=='w'||usermenu.freadonly=='wc')?'files':''}" id="${(usermenu.freadonly=='w'||usermenu.freadonly=='wc')?'idFile':''}"></a><img id="idProcess" style="display:none;" src="${rc.contextPath}/images/loading.gif" /></td>
	      </tr>
	      <tr>
	      	<td align="right" width="15%" style="line-height:35px;">资源类型：</td>
	      	<td>
	      		<select name="fresourcetype" onchange="isImage(this.value);">
	      			<option value="图片">图片资源&nbsp;&nbsp;&nbsp;</option>
	      			<option value="数据">数据资源&nbsp;&nbsp;&nbsp;</option>
	      		</select>
	      	</td>
	      </tr>
	      <tr>
	      	<td align="right" width="15%" style="line-height:35px;">站点名称：</td>
	      	<td>
	      		<select name="fsiteid" onchange="loadColumnSource(this)">
	      				<option value="">------选择站点------</option>
	      			<c:forEach var="info" items="${infos}">
		      				<option value="${info.id}">${info.ftitle}&nbsp;&nbsp;&nbsp;</option>
		      		</c:forEach>
	      		</select>
	      	</td>
	      </tr>
	      <tr>
	      	<td align="right" width="15%" style="line-height:35px;">关键字：</td>
	      	<td>
	      		<div id="image">
		      		<select name="fsearchkey" onchange="appendText(this.value)">
		      			<%-- <c:forEach var="search" items="${searchKeys}">
		      				<option value="${search}">${search}&nbsp;&nbsp;&nbsp;</option>
		      			</c:forEach> --%>
		      			<option value="模版背景图">模版背景图</option>
		      			<option value="资源图片">资源图片</option>
		      			<option value="相册">相册</option>
		      			<option value=""></option>
		      		</select>
		      		<span id="search_key_span" style="display:none;"><input type="text" name="searchText" id="searchText" value="" onchange="setSearchKeyVal(this.value);"/></span>
	      		</div>
	      		<div id="data" style="display: none;">
	      			<input name="searchKey" value=""/>
	      		</div>
      			<c:if test="${empty searchKeys}">
      				<script>appendText("");</script>
      			</c:if>
	      	</td>
	      </tr>
	      <tr id="targetColumn_tr" style="display:none;">
	      	<td align="right" width="15%" style="line-height:35px;">选择目标栏目：</td>
	      	<td>
	      		<div id="targetColumn_box"></div>
	      	</td>
	      </tr>
	      <tr>
	        <td colspan="2">
	        	<table border="0" cellspacing="0">
		            <thead>
		              <tr>
		                <td>待上传文件列表</td>
		                <td width="100"></td>
		              </tr>
		            </thead>
		            <tbody id="idFileList"></tbody>
	          </table>
	        </td>
	      </tr>
	      <tr>
	        <td colspan="2" style="color:gray">温馨提示：最多可同时上传 <b id="idLimit" style="color:red;"></b> 个文件，只允许上传 <b id="idExt"></b> 文件。(文件大小限制<font color="red">800KB</font>以下) </td>
	      </tr>
	      <tr>
	        <td colspan="2" align="center" id="idMsg"><input type="button" value="开始上传" onclick="upload()" id="idBtnupload" disabled="disabled" />
	          &nbsp;&nbsp;&nbsp;
	          <input type="button" value="全部取消" id="idBtndel" disabled="disabled" />
	        </td>
	      </tr>
	    </tbody>
	  </table>
	</form>
</body>
<script type="text/javascript">
var isIE = (document.all) ? true : false;

var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);


if (browser == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE9.0") {
    isIE = false;
}
if (browser == "Microsoft Internet Explorer" && version > 4) {
    isIE = false;
}
var $$ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    };
  }
};

var Extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
};

var Bind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	};
};

var Each = function(list, fun){
	for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i);}
};

var image = ["gif","jpg","png"];
//文件上传类
var FileUpload = Class.create();
FileUpload.prototype = {
  //表单对象，文件控件存放空间
  initialize: function(form, folder, options) {
	
	this.Form = $$(form);//表单
	this.Folder = $$(folder);//文件控件存放空间
	this.Files = [];//文件集合
	
	this.SetOptions(options);
	
	this.FileName = this.options.FileName;
	this._FrameName = this.options.FrameName;
	this.Limit = this.options.Limit;
	this.Distinct = !!this.options.Distinct;
	this.ExtIn = this.options.ExtIn;
	this.ExtOut = this.options.ExtOut;
	
	this.onIniFile = this.options.onIniFile;
	this.onEmpty = this.options.onEmpty;
	this.onNotExtIn = this.options.onNotExtIn;
	this.onExtOut = this.options.onExtOut;
	this.onLimite = this.options.onLimite;
	this.onSame = this.options.onSame;
	this.onFail = this.options.onFail;
	this.onIni = this.options.onIni;
	
	if(!this._FrameName){
		//为每个实例创建不同的iframe
		this._FrameName = "uploadFrame_" + Math.floor(Math.random() * 1000);
		//ie不能修改iframe的name
		var oFrame = isIE ? document.createElement("<iframe name='" + this._FrameName + "'>") : document.createElement("iframe");
		//为ff设置name
		oFrame.name = this._FrameName;
		oFrame.style.display = "none";
		//在ie文档未加载完用appendChild会报错
		document.body.insertBefore(oFrame, document.body.childNodes[0]);
	}
	
	//设置form属性，关键是target要指向iframe
	this.Form.target = this._FrameName;
	this.Form.method = "post";
	//注意ie的form没有enctype属性，要用encoding
	this.Form.encoding = "multipart/form-data";

	//整理一次
	this.Ini();
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
		FileName:	"filename",//文件上传控件的name，配合后台使用
		FrameName:	"",//iframe的name，要自定义iframe的话这里设置name
		onIniFile:	function(){},//整理文件时执行(其中参数是file对象)
		onEmpty:	function(){},//文件空值时执行
		Limit:		0,//文件数限制，0为不限制
		onLimite:	function(){},//超过文件数限制时执行
		Distinct:	true,//是否不允许相同文件
		onSame:		function(){},//有相同文件时执行
		ExtIn:		["gif","jpg","rar","zip","swf","png"],//允许后缀名
		onNotExtIn:	function(){},//不是允许后缀名时执行
		ExtOut:		[],//禁止后缀名，当设置了ExtIn则ExtOut无效
		onExtOut:	function(){},//是禁止后缀名时执行
		onFail:		function(){},//文件不通过检测时执行(其中参数是file对象)
		onIni:		function(){}//重置时执行
    };
    Extend(this.options, options || {});
  },
  //整理空间
  Ini: function() {
	//整理文件集合
	this.Files = [];
	//整理文件空间，把有值的file放入文件集合
	Each(this.Folder.getElementsByTagName("input"), Bind(this, function(o){
		if(o.type == "file"){ o.value && this.Files.push(o); this.onIniFile(o); }
	}));
	//插入一个新的file
	var file = document.createElement("input");
	file.name = this.FileName; file.type = "file"; file.onchange = Bind(this, function(){ this.Check(file); this.Ini(); });
	this.Folder.appendChild(file);
	//执行附加程序
	this.onIni();
  },
  //检测file对象
  Check: function(file) {
	//检测变量
	var bCheck = true;
	//空值、文件数限制、后缀名、相同文件检测
	if(!file.value){
		bCheck = false; this.onEmpty();
	} else if(this.Limit && this.Files.length >= this.Limit){
		bCheck = false; this.onLimite();
	} else if(!!this.ExtIn.length && !RegExp("\.(" + this.ExtIn.join("|") + ")$$", "i").test(file.value)){
		//检测是否允许后缀名
		bCheck = false; this.onNotExtIn();
	} else if(!!this.ExtOut.length && RegExp("\.(" + this.ExtOut.join("|") + ")$$", "i").test(file.value)) {
		//检测是否禁止后缀名
		bCheck = false; this.onExtOut();
	} else if(!!this.Distinct) {
		Each(this.Files, function(o){ if(o.value == file.value){ bCheck = false; }});
		if(!bCheck){ this.onSame(); }
	}
	//没有通过检测
	!bCheck && this.onFail(file);
  },
  //删除指定file
  Delete: function(file) {
	//移除指定file
	this.Folder.removeChild(file); this.Ini();
  },
  //删除全部file
  Clear: function() {
	//清空文件空间
	Each(this.Files, Bind(this, function(o){ this.Folder.removeChild(o); })); this.Ini();
  }
};

var fu = new FileUpload("uploadForm", "idFile", { 
	onIniFile: function(file){ file.value ? file.style.display = "none" : this.Folder.removeChild(file); },
	onEmpty: function(){ alert("请选择一个文件"); },
	onLimite: function(){ alert("超过上传限制"); },
	onSame: function(){ alert("已经有相同文件"); },
	onNotExtIn:	function(){ alert("只允许上传" + this.ExtIn.join("，") + "文件"); },
	onFail: function(file){ this.Folder.removeChild(file); },
	onIni: function(){
		//显示文件列表
		var arrRows = [];
		if(this.Files.length){
			var oThis = this;
			Each(this.Files, function(o){
				var a = document.createElement("a"); a.innerHTML = "取消"; a.href = "?";
				a.onclick = function(){ oThis.Delete(o); return false; };
				var val = o.value;
				var name = val.substring(val.lastIndexOf("//")+1, val.length);
				/* for(var i = 0; i <image.length;i++){
					if(suffix == image[i]){
						val = '<img src="'+val+'" width="60" height="50">';
					}
				} */
				arrRows.push([name, a]);
			});
		} else { arrRows.push(["<font color='gray'>没有添加文件</font>", "&nbsp;"]); }
		AddList(arrRows);
		//设置按钮
		$$("idBtnupload").disabled = $$("idBtndel").disabled = this.Files.length <= 0;
	}
});

function upload(){
	var res = !($("select[name='fsearchkey']").val() == '相册' && validateColumn().size() == 0);
	var colId = '';
	validateColumn().each(function(){
		colId += ';' + $(this).val();
	});
	$("#columnIdStr").val(colId);
	
	if(res){
		
		//显示文件列表
		var arrRows = [];
		Each(fu.Files, function(o){ arrRows.push([o.value, "&nbsp;"]); });
		AddList(arrRows);
		
		fu.Folder.style.display = "none";
		$$("idProcess").style.display = "";
		$$("idMsg").innerHTML = "正在上传文件到服务器中，请稍候……<br />有可能因为网络问题，出现程序长时间无响应，请点击“<a href='?'><font color='red'>取消</font></a>”重新上传文件";
		
		fu.Form.submit();
	}else{
		$.growlUI('请检查是否选择栏目');
	} 
};

function validateColumn(){
	return $("input[type='checkbox'][name='columnId']:checked");
}

//用来添加文件列表的函数
function AddList(rows){
	//根据数组来添加列表
	var FileList = $$("idFileList"), oFragment = document.createDocumentFragment();
	//用文档碎片保存列表
	Each(rows, function(cells){
		var row = document.createElement("tr");
		Each(cells, function(o){
			var cell = document.createElement("td");
			if(typeof o == "string"){ cell.innerHTML = o;}else{ cell.appendChild(o);}
			row.appendChild(cell);
		});
		oFragment.appendChild(row);
	});
	//ie的table不支持innerHTML所以这样清空table
	while(FileList.hasChildNodes()){ FileList.removeChild(FileList.firstChild); }
	FileList.appendChild(oFragment);
}

$$("idLimit").innerHTML = fu.Limit == 0 ? "任意多" : fu.Limit;

$$("idExt").innerHTML = fu.ExtIn.join("，");

$$("idBtndel").onclick = function(){ fu.Clear(); };

//在后台通过window.parent来访问主页面的函数
function Finish(msg){ 
	msg=msg.replace(/<br[^>]*>/ig,"\n").replace(/&nbsp;/ig," ");
	//alert(msg); 
	//location.href = location.href;
	//createTable();
	$$("idMsg").innerHTML = '<input type="button" value="开始上传" id="idBtnupload" onclick="upload()" disabled="disabled" />&nbsp;&nbsp;&nbsp;<input type="button" value="全部取消" id="idBtndel" disabled="disabled" />';
	fu.Folder.style.display = "";
	$$("idProcess").style.display = "none";
	fu.Clear();
	$.growlUI('提示',msg); 
}
</script>
</html>
